//
//  CLMineChooseUnitCollectionViewCell.swift
//  CLEasyBBQ
//
//  Created by simon zhao on 2017/4/21.
//  Copyright © 2017年 simon zhao. All rights reserved.
//

import UIKit

typealias selectClousure = (_ cellTag:Int) -> Void

class CLMineChooseUnitCollectionViewCell: UICollectionViewCell {
    
    var cellBtn : UIButton!
    var isSelec = false
    var UnitSeletedClousure : selectClousure!
    
    
//    init(status : Bool) {
//        
//        super.init(status: status)
//        cellBtn = UIButton()
//        cellBtn.layer.cornerRadius = 5
//        cellBtn.addTarget(self, action: #selector(changeStatus), for: .touchUpInside)
//        
//        
//        if status == true {
//            
//            cellBtn.backgroundColor = UIColor.red
//        }else{
//            
//            cellBtn.backgroundColor = UIColor.green
//        }
//        
//    }
    
    
    override init(frame: CGRect) {
        
        
        super.init(frame: frame)
        
        print(frame)
        cellBtn = UIButton()
        cellBtn.layer.cornerRadius = 5
        cellBtn.addTarget(self, action: #selector(changeStatus), for: .touchUpInside)
        self.contentView.addSubview(cellBtn)
        
        
    }
    
    func changeStatus (sender : UIButton) {
        
        sender.isSelected = !sender.isSelected
        if sender.isSelected  == true {
            
            cellBtn.backgroundColor = UIColor.red
            
            let colView = sender.superview?.superview?.superview as? UICollectionView
            let cell = sender.superview?.superview as! CLMineChooseUnitCollectionViewCell
            let index = colView?.indexPath(for: cell)
            
            if UnitSeletedClousure != nil {
                
                UnitSeletedClousure(index!.item)
            }
            
            
        }else {
            
            cellBtn.backgroundColor = UIColor.green
        }
        
        
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        cellBtn.snp.makeConstraints { (make) in
            
            make.top.equalTo(contentView).offset(10)
            make.bottom.equalTo(contentView).offset(-10)
            make.width.equalTo(contentView)
            make.left.equalTo(contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
